﻿using OestsCommon.Model;
using System;
using System.Data;
using System.Data.SqlClient;

namespace OestsDataBase
{
    public class AdminDAL
    {
        //管理
        public static void CreateTable()
        {
            string sqlCheck = "if object_id( 'T_Admin') is not null select 1 else select 0";
            if ((int)SqlHelper.ExecuteScalar(sqlCheck) == 0)
            {
                string sqlCreacte = @"CREATE TABLE T_Admin (id bigint identity(1,1) ,
name NVARCHAR(20) primary key,password NVARCHAR(20),login_time datetime,logout_time datetime) ";
                SqlHelper.ExecuteNonQuery(sqlCreacte);

                string strSql = string.Format("insert into T_Admin (name, password) values('{0}','{1}');SELECT @@Identity", "Admin", "");
                SqlHelper.ExecuteScalar(strSql);
            }
        }

        /// <summary>
        /// 根据工号获得一个Amin 没有返回空
        /// </summary>
        /// <param name="number"></param>
        /// <returns></returns>
        public static Admin GetAdminByNumber(string number)
        {
            string strSql = "select * from T_Admin where number=@number";
            DataTable dt = SqlHelper.ExecuteDataTable(strSql, new SqlParameter("@number", number));

            if (dt == null || dt.Rows.Count == 0)
            {
                return null;
            }
            return ReadDataRow(dt.Rows[0]);
        }

        private static Admin ReadDataRow(DataRow dr)
        {
            Admin a = new Admin
            {
                DBid = Int32.Parse(dr["id"].ToString()),
                Type = UserType.Admin,
                Number = dr["number"].ToString(),
                Name = dr["name"].ToString(),
                Password = dr["password"].ToString(),
                Environment = (TrainingEnvironment)(dr["environment"].Equals(DBNull.Value) ? 0 : Int32.Parse(dr["environment"].ToString())),
                ExamProjectId = dr["examProjectId"].Equals(DBNull.Value) ? 0 : Int32.Parse(dr["examProjectId"].ToString()),
                ExamPaperId = dr["examPaperId"].Equals(DBNull.Value) ? 0 : Int32.Parse(dr["examPaperId"].ToString()),
            };
            return a;
        }

        public static void SaveLoginTime(int id)
        {
            DateTime date = DateTime.Now;
            string strSql = string.Format("update T_Admin set login_time='{0}' where id = @id", date);
            int n = SqlHelper.ExecuteNonQuery(strSql, new SqlParameter("@id", id));
        }

        public static void SaveLogoutTime(int id)
        {
            DateTime date = DateTime.Now;
            string strSql = string.Format("update T_Admin set logout_time='{0}' where id = @id", date);
            int n = SqlHelper.ExecuteNonQuery(strSql, new SqlParameter("@id", id));
        }




        /// <summary>
        /// 只有一个管理员 不需要id
        /// </summary>
        /// <param name="pwd"></param>
        /// <returns></returns>
        public static bool ModifyPwd(string pwd)
        {
            string strSql = string.Format("update T_User set password='{0}' where user_type='{1}'", pwd, UserType.Admin.ToString());
            int n = SqlHelper.ExecuteNonQuery(strSql);
            if (n != 0)
            {
                return true;
            }
            return false;
        }

        public static bool SetTrainingEnvironment(TrainingEnvironment te, int projectId, int paperId = 0)
        {
            string strSql = string.Format("update T_Admin set environment={0},examProjectId ={1},examPaperId = {2}", (int)te, projectId, paperId);
            int n = SqlHelper.ExecuteNonQuery(strSql);
            if (n != 0)
            {
                return true;
            }
            return false;
        }

        public static DP<TrainingEnvironment, int, int> GetTrainingEnvironment()
        {
            DP<TrainingEnvironment, int, int> data = new DP<TrainingEnvironment, int, int>();

            string strSql = "select environment,examProjectId,examPaperId from T_Admin where id=1";
            DataTable dt = SqlHelper.ExecuteDataTable(strSql);
            if (dt == null || dt.Rows.Count == 0)
            {
                return null;
            }
            else
            {
                data.Value1 = (TrainingEnvironment)Int32.Parse(dt.Rows[0]["environment"].ToString());
                data.Value2 = Int32.Parse(dt.Rows[0]["examProjectId"].ToString());
                data.Value3 = Int32.Parse(dt.Rows[0]["examPaperId"].ToString());
            }
            return data;
        }

        public static int GetPaperId()
        {
            string strSql = "select examPaperId from T_Admin where id=1";
            Object obj = SqlHelper.ExecuteScalar(strSql);
            if (obj == null) return 0;
            return (int)obj;
        }
    }
}
